class AnswerHandle {
    checkpoints = []
    constructor(checkpoints) {
        this.checkpoints = checkpoints
        this._getCheckpointsLock()
    }

    handleCheckpoints() {
        let arr = []
        for (let i = 0; i < this.checkpoints.length; i++) {
            if (i===0 || i===1 || i===2) {
                this.checkpoints[i].prePercent = null
            }else {
                this.checkpoints[i].prePercent = this.checkpoints[i - 1].percent
            }
            arr.push(this.checkpoints[i])
        }
        console.log(arr)
        return arr
    }

    _getCheckpointsLock(){
        for (let i = 0; i < this.checkpoints.length; i++) {
            const best = this._getMax(this.checkpoints[i].user_activity_logs)
            this.checkpoints[i].best = best
            this.checkpoints[i].percent = this._getCpsPer(best, i)
        }
    }

    _getCpsPer(best, i) {
        const count = (i+1) * 5
        if (!best) {
            return 0
        }
        return ((best.point / 2) / count).toFixed(2)
    }

    _getMax(logs=[]) {
        if (logs.length === 0) {
            return null
        }
        return  logs.sort(function (a, b) {return ((b.duration+b.point) - (a.duration+a.point))})[0];
    }
}
export {
    AnswerHandle
}
